package com.hera.process.web.primefaces.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hera.base.util.Constantes;
import com.hera.base.util.FiltroDTO;
import com.hera.base.util.WrapperDate;
import com.hera.process.config.rest.delegate.ExternalDelegate;
import com.hera.process.config.rest.delegate.FormDelegate;
import com.hera.process.config.rest.delegate.WorkflowDelegate;
import com.hera.process.form.domain.Dynaform;
import com.hera.process.form.domain.DynaformField;
import com.hera.process.form.domain.DynaformStage;
import com.hera.process.form.domain.FieldOption;
import com.hera.process.form.domain.FormField;
import com.hera.process.workflow.domain.Process;
import com.hera.process.workflow.domain.Stage;
import com.hera.process.workflow.domain.Swimlane;
import com.hera.process.workflow.domain.TaskInstanceCache;


@Component
@SessionScoped
@ManagedBean
public class StageController {

	@Autowired
	public WorkflowDelegate workflowDelegate;
	
	@Autowired
	public FormDelegate formDelegate;
	
	@Autowired
	public ExternalDelegate externalDelegate;
	
	private List<Stage> stages;
	private List<DynaformField> dynaformfields;
	private List<FieldOption> fieldoptions;
	private Stage currentStage;
	private TaskInstanceCache currentTaskInstance;
	private Long currentIdTask;
	/*
	public List<Stage> lstStage(){
		
		try {
			Long idProceso = Long.parseLong((FacesContext.getCurrentInstance(). getExternalContext().getRequestParameterMap().get("idProceso")));
			
			
			Stage stage = new Stage();
			Swimlane swimlane = new Swimlane();
			Process process = new Process();
			process.setIdProcess(idProceso);
			process.setIdType(Long.valueOf(Constantes.NODETYPE.ID_BEGIN));
			swimlane.setProcess(process);
			stage.setSwimlane(swimlane);
			
			List<Stage> stages=workflowDelegate.findStageByProcessIdandTypeId(stage);
			System.out.println("Lista de Stages"+stages.size());
			FacesContext.getCurrentInstance().getExternalContext().redirect("stage.xhtml");
			
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		return  stages;
	}*/
	
	public void fillInitialForm(){
		
		Long idProceso = Long.parseLong((FacesContext.getCurrentInstance(). getExternalContext().getRequestParameterMap().get("idProceso")));
		
		
		Stage stage = new Stage();
		Swimlane swimlane = new Swimlane();
		Process process = new Process();
		process.setIdProcess(idProceso);
		process.setIdType(Long.valueOf(Constantes.NODETYPE.ID_BEGIN));
		swimlane.setProcess(process);
		stage.setSwimlane(swimlane);
		
		List<Stage> stages = workflowDelegate.findStageByProcessIdandTypeId(stage);
		currentStage = stages.get(0);
		stage.setIdStage(stages.get(0).getIdStage());
		List<DynaformStage> lstDynaformStages = formDelegate.findAllDynaformStagesByStage(stage);
		
		FiltroDTO filtroDTO = new FiltroDTO();
		filtroDTO.addParametro("idDynaform", lstDynaformStages.get(0).getDynaform().getIdDynaform());
		List<Dynaform> lstDynaForms = formDelegate.findAllDynaforms(filtroDTO);
		
		Dynaform dynaForm = new Dynaform();
		dynaForm.setIdDynaform(lstDynaForms.get(0).getIdDynaform());
		
		
		FiltroDTO filtroDTO1 = new FiltroDTO();
		filtroDTO1.addParametro("idDynaform", dynaForm.getIdDynaform());

		
		dynaformfields = formDelegate.findAllDynaFormFieldByIdDynaForm(filtroDTO1);
		
		for (DynaformField dynaformfield : dynaformfields) {
			if(Constantes.FORMTYPECOMPONENT.COMBOBOX==dynaformfield.getIdType()||Constantes.FORMTYPECOMPONENT.OPTIONGROUP==dynaformfield.getIdType()){
				FiltroDTO filtroDTO2= new FiltroDTO();
				filtroDTO2.addParametro("formField.idField", dynaformfield.getField().getIdField());
				dynaformfield.getField().setFieldOptionList(formDelegate.findAllFieldOptions(filtroDTO2));
				//System.out.println(dynaformfield.getField().getIdField() + " size: "+dynaformfield.getField().getFieldOptionList().size());
			}
		}
		
		
	
		//System.out.println(listFormFields.size());
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("form.xhtml");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//return dynaformfields;
		
	}
	
	public List<FieldOption> lstFieldOptions(){
		Long idDynaForm=dynaformfields.get(0).getDynaform().getIdDynaform();
		List<FormField> lstformfield=formDelegate.findAllFormFieldByDynaForm(idDynaForm);
		
		FiltroDTO filtroDTO2= new FiltroDTO();
		filtroDTO2.addParametro("formField.idField", lstformfield.get(0).getIdField());
		fieldoptions =formDelegate.findAllFieldOptions(filtroDTO2);
		for (FieldOption fieldOption : fieldoptions) {
				System.out.println("/n"+fieldOption.getValue());
				System.out.println("a");
		}
		return fieldoptions;
		
	}
	
	public void irProcesos(){
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("proceso.xhtml");
		} catch (IOException e) {
			
			e.printStackTrace();
		}		
	}
	
	public void saveTarea(){
			
		Map<String, Object> entidad = new HashMap<String, Object>();
		for (DynaformField dynaformfield : dynaformfields) {
			if(dynaformfield.getFormValue() instanceof Date){				
				entidad.put(dynaformfield.getField().getAddTableDetail().getName(), new WrapperDate((Date)dynaformfield.getFormValue()));
			}else{
				entidad.put(dynaformfield.getField().getAddTableDetail().getName(), dynaformfield.getFormValue());
			}
		}
		
		entidad.put(Constantes.STRINGVALUES.STRSTAGE, currentStage);
		//grabando la entidad
		//externalDelegate.insertMetadata(entidad);
		workflowDelegate.createInstanceProcess(entidad);
		try{
			FacesContext.getCurrentInstance().getExternalContext().redirect("bandeja.xhtml");
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	public List<Stage> getStages() {
		return stages;
	}

	public void setStages(List<Stage> stages) {
		this.stages = stages;
	}



	public List<DynaformField> getDynaformfields() {
		return dynaformfields;
	}

	public void setDynaformfields(List<DynaformField> dynaformfields) {
		this.dynaformfields = dynaformfields;
	}

	public List<FieldOption> getFieldoptions() {
		return fieldoptions;
	}

	public void setFieldoptions(List<FieldOption> fieldoptions) {
		this.fieldoptions = fieldoptions;
	}
	
	public void fillFormByStage(){
		
		
		//System.out.println(currentIdTask);
		currentIdTask = Long.parseLong((FacesContext.getCurrentInstance(). getExternalContext().getRequestParameterMap().get("idTask")));
		FiltroDTO filtroDTO3 = new FiltroDTO();
		filtroDTO3.addParametro("idTask",currentIdTask);
		List<TaskInstanceCache> listTask = workflowDelegate.findAllTaskInstanceCaches(filtroDTO3);
		if(listTask != null && listTask.size() > 0){
			currentTaskInstance = listTask.get(0);
			
			Stage stage = new Stage();
			stage.setIdStage(currentTaskInstance.getIdStage());
			List<DynaformStage> lstDynaformStages = formDelegate.findAllDynaformStagesByStage(stage);
			FiltroDTO filtroDTO = new FiltroDTO();
			filtroDTO.addParametro("idDynaform", lstDynaformStages.get(0).getDynaform().getIdDynaform());
			List<Dynaform> lstDynaForms = formDelegate.findAllDynaforms(filtroDTO);
			
			Dynaform dynaForm = new Dynaform();
			dynaForm.setIdDynaform(lstDynaForms.get(0).getIdDynaform());
					
			FiltroDTO filtroDTO1 = new FiltroDTO();
			filtroDTO1.addParametro("idDynaform", dynaForm.getIdDynaform());
			
			dynaformfields = formDelegate.findAllDynaFormFieldByIdDynaForm(filtroDTO1);
			List<Object> fields = new ArrayList<Object>();
			fields.add(currentTaskInstance.getIdExtReference());
			
			for (DynaformField dynaformfield : dynaformfields) {
				if(Constantes.FORMTYPECOMPONENT.COMBOBOX==dynaformfield.getIdType()||Constantes.FORMTYPECOMPONENT.OPTIONGROUP==dynaformfield.getIdType()){
					FiltroDTO filtroDTO2= new FiltroDTO();
					filtroDTO2.addParametro("formField.idField", dynaformfield.getField().getIdField());
					dynaformfield.getField().setFieldOptionList(formDelegate.findAllFieldOptions(filtroDTO2));
				}
				fields.add(dynaformfield.getField().getAddTableDetail().getName());
			}
			
			Map<String, Object> formValues = externalDelegate.selectMetadataById(fields);
			
			for (DynaformField dynaformfield : dynaformfields) {
				
				dynaformfield.setFormValue(formValues.get(dynaformfield.getField().getAddTableDetail().getName()));
			}
			
			try {
				FacesContext.getCurrentInstance().getExternalContext().redirect("form.xhtml");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public void updateTarea(){
		
		Map<String, Object> entidad = new HashMap<String, Object>();
		for (DynaformField dynaformfield : dynaformfields) {
			if(dynaformfield.getFormValue() instanceof Date){				
				entidad.put(dynaformfield.getField().getAddTableDetail().getName(), new WrapperDate((Date)dynaformfield.getFormValue()));
			}else{
				entidad.put(dynaformfield.getField().getAddTableDetail().getName(), dynaformfield.getFormValue());
			}
		}
		
		entidad.put(Constantes.STRINGVALUES.STRTASKINSTANCE, currentTaskInstance);
		//grabando la entidad
		workflowDelegate.udpateInstanceProcess(entidad);
		try{
			FacesContext.getCurrentInstance().getExternalContext().redirect("bandeja.xhtml");
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

	public TaskInstanceCache getCurrentTaskInstance() {
		return currentTaskInstance;
	}

	public void setCurrentTaskInstance(TaskInstanceCache currentTaskInstance) {
		this.currentTaskInstance = currentTaskInstance;
	}



}
